//
//  JHLoginViewController.m
//  login
//
//  Created by 蒋梦婷 on 16/9/24.
//  Copyright © 2016年 code. All rights reserved.
//

#import "JHLoginViewController.h"
#import "JHRegisterViewController.h"

//#import "JHPassWordViewController.h"
#import "SVProgressHUD.h"
//#import "SIXUserModel.h"
//#import "SIXUserInformation.h"
//#import "SIXLoadViewController.h"
//#import "SNLoginModel.h"
#import "GHNetWorkTools.h"
#import "BaseNavigationController.h"
#import "DrawerViewController.h"
#import "LeftMenuTableViewController.h"
#import "HomeViewController.h"
#import "LeftMenuLogedInTableViewController.h"
#import "DefineTools.h"
//#import "CZLoopImagesController.h"
//#import "LoopImageViewController.h"
#import "JHLoginViewController.h"
#import "UserInfoModel.h"
#import <RongIMKit/RongIMKit.h>

@interface JHLoginViewController ()
@property (weak, nonatomic) IBOutlet UITextField *username;//手机号
@property (weak, nonatomic) IBOutlet UITextField *passWord;//登录密码
@property (weak, nonatomic) IBOutlet UISegmentedControl *userRight;//用户权限
@property (weak, nonatomic) IBOutlet UIButton *loginBtn;//登录按钮
@property (weak, nonatomic) IBOutlet UIButton *registerBtn;

@property (weak, nonatomic) IBOutlet UIButton *forgetPassWordBtn;
@property (nonatomic, strong) UserInfoModel *userinfo;
@end

@implementation JHLoginViewController



- (void)viewDidLoad {
    [super viewDidLoad];
    
    [[RCIM sharedRCIM] connectWithToken:@"48LdO/pHANWH62PXeiwwcZcp4syqkKmXJduzCcKxG1w14fi0Q83YqxpUbH4fElO/cYMSw6i4M5v+OuA/1/WtQQ==" success:^(NSString *userId) {
        NSLog(@"登陆成功。当前登录的用户ID：%@", userId);
        
        //[self.navigationController pushViewController:chat animated:YES];
    } error:^(RCConnectErrorCode status) {
        NSLog(@"登陆的错误码为:%ld", status);
        
        //[self dismissViewControllerAnimated:YES completion:nil];
    } tokenIncorrect:^{
        //token过期或者不正确。
        //如果设置了token有效期并且token过期，请重新请求您的服务器获取新的token
        //如果没有设置token有效期却提示token错误，请检查您客户端和服务器的appkey是否匹配，还有检查您获取token的流程。
        NSLog(@"token错误");
       // [self dismissViewControllerAnimated:YES completion:nil];
        
    }];
    
    
    
    
    
    
    self.view.backgroundColor=[UIColor whiteColor];
    self.username.text = @"";
    
    self.passWord.text = @"";
    
   self.username.text = @"15739570841";
    
    self.passWord.text = @"c39es2";
    
    self.userRight = NO;
    
    [self setNav];
    
    //[[NSNotificationCenter defaultCenter]postNotificationName:@"haha" object:@"VC"];
    
    
}

- (void)setNav{
    self.title = @"用户登录";
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"xianhua"] style:UIBarButtonItemStylePlain target:self action:@selector(backToLeftView)];
    self.navigationItem.leftBarButtonItem.tintColor = [UIColor whiteColor];
    
}

- (void)backToLeftView{
    
    [self dismissViewControllerAnimated:YES completion:nil];
    
    
}

#pragma mark -
#pragma mark - 点击 登录 按钮 跳转页面
- (IBAction)clickLoginBtn:(id)sender {

    
    if (self.username.text.length != 11) {
        [SVProgressHUD showErrorWithStatus:@"请输入正确的手机号" ];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [SVProgressHUD dismiss];
        });
        return;
        
    } if (self.passWord.text.length != 0) {
        [self.view endEditing:YES];
        
        
        
        
        
        
        NSString *url = @"denglu";
        
        NSInteger i = self.userRight.selectedSegmentIndex;
       // NSString *ro =
        NSDictionary *paramDic = @{@"username":[_username text],@"password":[_passWord text],@"role":@(i)};
        NSLog(@"username--------:%@",_username.text);
        
        [[GHNetWorkTools sharedTools] GET:url parameters:paramDic progress:^(NSProgress * _Nonnull downloadProgress) {
            NSLog(@"downing--------:");
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            
           // NSLog(@"susses----:%@",responseObject);
            
            NSDictionary *result = [[NSDictionary alloc] init];
            
            if ([responseObject isKindOfClass:[NSData class]]){
                
                result = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
                
            }else{
                
                result = responseObject;
            }
            
          
            
            NSString *codestr = [result valueForKey:@"code"];
            int code = [codestr intValue];
            NSLog(@"result----ggggggggggg:%d",code);
            if (code == 0) {
                [SVProgressHUD showErrorWithStatus:[result objectForKey:@"msg"]];
            }
            else{
             //   [self setRootVC];
                /*
                 @property (nonatomic ,assign) NSInteger isLogin;
                 @property (nonatomic ,copy) NSString *username;
                 @property (nonatomic ,copy) NSString *userid;
                 @property (nonatomic ,assign) NSInteger heigh;
                 @property (nonatomic ,copy) NSString *token;
                 @property (nonatomic ,copy) NSString *phonenumber;
                 @property (nonatomic ,assign) NSInteger age;
                 @property (nonatomic ,assign) NSInteger sex;
                 @property (nonatomic ,assign) NSInteger weigh;
                 @property (nonatomic ,copy) NSString *passwords;
                 @property (nonatomic ,copy) NSString *roles;
                 @property (nonatomic ,copy) NSString *userIdCard;
                 @property (nonatomic ,copy) NSString *headimage;
                 @property (nonatomic ,copy) NSString *useraddress;
                 @property (nonatomic ,assign) NSInteger userStatus;

                 */
                NSLog(@"%@",[result allKeys]);
                [SVProgressHUD showSuccessWithStatus:[result objectForKey:@"msg"]];
                [UserInfoModel shareUserInfoModel].isLogin = 1;
                
                
                
                [UserInfoModel shareUserInfoModel].roles = [result valueForKey:@"roles"];
                NSString *temp = [result valueForKey:@"weigh"];
                [UserInfoModel shareUserInfoModel].weigh = [temp integerValue];
                [UserInfoModel shareUserInfoModel].sex = [result valueForKey:@"sex"];
                NSLog(@"%@",[result valueForKey:@"userid"]);
                [UserInfoModel shareUserInfoModel].userid = [[result valueForKey:@"userid"] integerValue];
                [UserInfoModel shareUserInfoModel].phonenumber = [result valueForKey:@"phonenumber"];
                 [UserInfoModel shareUserInfoModel].headimage = [result valueForKey:@"headimage"];
                 [UserInfoModel shareUserInfoModel].age = [[result valueForKey:@"age"] integerValue];
                 [UserInfoModel shareUserInfoModel].userIdCard = [result valueForKey:@"userIdCard"];
                 [UserInfoModel shareUserInfoModel].useraddress = [result valueForKey:@"useraddress"];
                [UserInfoModel shareUserInfoModel].userStatus = [[result valueForKey:@"userStatus"] integerValue];
                [UserInfoModel shareUserInfoModel].token = [result valueForKey:@"token"];
                [UserInfoModel shareUserInfoModel].heigh = [[result valueForKey:@"heigh"] integerValue];
                [UserInfoModel shareUserInfoModel].username = [result valueForKey:@"username"];
                [UserInfoModel shareUserInfoModel].passwords = [result valueForKey:@"passwords"];
                //[UserInfoModel]
                
                
                [[NSNotificationCenter defaultCenter] postNotificationName:@"setRootVC" object:nil];
               
            }
            
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            NSLog(@"fail--------:");
        }];
    }
}


#pragma mark -
#pragma mark - 点击注册 按钮
- (IBAction)clickRegisterBtn:(id)sender {
    JHRegisterViewController *registerVC = [[JHRegisterViewController alloc] init];
    [self.navigationController pushViewController:registerVC animated:YES];
    
}

//#pragma mark -
//#pragma mark - 点击 忘记密码
//- (IBAction)clickForgetPasswordBtn:(id)sender {
//    JHPassWordViewController *passwordVC  = [[SIXPassWordViewController alloc] init];
//    [self.navigationController pushViewController:passwordVC animated:YES];
//    
//}

//-(void)setRootVC{
//    //创建首页
//    HomeViewController * homeViewController = [[HomeViewController alloc]init];
//    // LoopImageViewController* homeViewController = [[LoopImageViewController alloc] init];
//    //JHLoginViewController * homeViewController = [[JHLoginViewController alloc] init];
//    BaseNavigationController * naVC =[[BaseNavigationController alloc]initWithRootViewController:homeViewController ];
//    //创建左侧菜单
//    
//    LeftMenuTableViewController * leftVC = [[LeftMenuTableViewController alloc]init];
//    
//    
//    //如果登陆了跳转到登陆后的视图(继承自访客控制器,继承属性)
//    if (_userinfo.isLogin) {
//        leftVC = [[LeftMenuLogedInTableViewController alloc] init ];
//    }
//    else {
//        leftVC = [[LeftMenuTableViewController alloc] init];
//    }
//    BaseNavigationController *baseVC = [[BaseNavigationController alloc] initWithRootViewController:leftVC];
//    
//    //实现抽屉效果
//    self.drawerVC= [[DrawerViewController alloc] initWithCenterViewController:naVC leftDrawerViewController:baseVC];
//    [self.drawerVC setShowsShadow:YES];
//    // 右控制器的初始宽度
//    [self.drawerVC setMaximumLeftDrawerWidth:KScreenWidth*0.75];
//    // 打开手势操作区域
//    [self.drawerVC setOpenDrawerGestureModeMask:MMOpenDrawerGestureModeAll];
//    // 关闭手势操作区域
//    [self.drawerVC setCloseDrawerGestureModeMask:MMCloseDrawerGestureModeAll];
//    //设置为主窗口的根控制器
//    self.window.rootViewController = self.drawerVC;
//    
//}
//



@end
